package com.example.jjc.rxjava_retrofit2_demo.module;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.jjc.rxjava_retrofit2_demo.BaseFragment;
import com.example.jjc.rxjava_retrofit2_demo.ImageActivity;
import com.example.jjc.rxjava_retrofit2_demo.R;
import com.example.jjc.rxjava_retrofit2_demo.adapter.ZhuangbiListAdapter;
import com.example.jjc.rxjava_retrofit2_demo.model.Item;
import com.example.jjc.rxjava_retrofit2_demo.network.Api;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/3/15.
 */

public class Fra_1 extends BaseFragment {
    @BindView(R.id.gridRv)
    RecyclerView gridRv;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;

    private ZhuangbiListAdapter adapter = new ZhuangbiListAdapter();
    private int position = 0;//点击的位置
    private boolean isFirst = true;//是否第一次打开该界面

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fra_1, container, false);
        ButterKnife.bind(this, view);

        initView();
        initData();
        isFirst = false;
        return view;
    }

    /**
     * 初始化视图
     */
    private void initView() {
//        gridRv.setLayoutManager(new GridLayoutManager(getActivity(),2));
        gridRv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        gridRv.setAdapter(adapter);
//        srl.setColorSchemeColors(Color.GREEN,Color.RED,Color.BLUE,Color.YELLOW);
        srl.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
//        srl.setEnabled(false);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
    }

    /**
     * 加载数据
     */
    private void initData() {
        unSubscription();
        if (isFirst){
            adapter.setImageList(null);
        }
        srl.setRefreshing(true);
        search("装逼");
    }

    /**
     * 请求网络数据
     * @param key 关键字
     */
    private void search(String key){//将本页面的观察者与被观察者建立起订阅关系
        //订阅关系
        subscription = Api.getZhuangbiApi()
                .search(key)//搜索数据
                .subscribeOn(Schedulers.io())//运行模式
                .observeOn(AndroidSchedulers.mainThread())//在主线程运行
                .subscribe(observer);//绑定观察者
    }

    //本页面的观察者
    Observer<List<Item>> observer = new Observer<List<Item>>() {
        @Override
        public void onCompleted() {
            Log.d("Fra_1","请求图片完成");
        }

        @Override
        public void onError(Throwable e) {
            srl.setRefreshing(false);
            Log.d("Fra_1","图片加载失败");
            Log.e("Fra_1",e.getMessage().toString());
        }

        @Override
        public void onNext(final List<Item> items) {
            srl.setRefreshing(false);
            Log.d("Fra_1","下一步，填充数据");
            adapter.setImageList(items);
            adapter.setOnItemClickListener(new ZhuangbiListAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view) {
                    Intent intent = new Intent(Fra_1.this.getActivity(), ImageActivity.class);
                    position = gridRv.getChildAdapterPosition(view);
                    intent.setData(Uri.parse(items.get(position).image_url));
                    startActivity(intent);
                }
            });
        }
    };

    /**
     * 拿到点击的位置
     */
    public int getPosition(){
        return position;
    }

    public List<Item> getList (){
        return adapter.getImageList();
    }

}
